﻿package cn.sdhuijin.healthexam.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.TextView;
import cn.sdhuijin.healthexam.R;
import cn.sdhuijin.healthexam.bean.UserData;
import cn.sdhuijin.healthexam.bean.OrganizationData;

public class ExListViewAdapter extends BaseExpandableListAdapter {
	public List<ArrayList<UserData>> userDatas;
	public List<OrganizationData> grouDatas;
	private OrganizationData gdata;
	private UserData cdata;
	private View tempView;
	private LayoutInflater mInflater = null;
	private OnClickListener onclickListener;
	private ChildViewHolder childHolder;
	private GroupViewHolder groupHolder;

	public ExListViewAdapter(Context ct, List<OrganizationData> gdatas,
			List<ArrayList<UserData>> cdatas, OnClickListener onclick) {
		mInflater = LayoutInflater.from(ct);
		grouDatas = gdatas;
		userDatas = cdatas;
		onclickListener = onclick;
	}

	public void addGroup(OrganizationData gdata) {
	}

	public void delGroup(int groupIndex) {

	}

	public void addChild(int groupIndex, UserData cdata) {
		if (userDatas.get(groupIndex) == null) {
			ArrayList<UserData> tempDatas = new ArrayList<UserData>();
			userDatas.set(groupIndex, tempDatas);
		}
		userDatas.get(groupIndex).add(cdata);

	}

	public void delChild(int groupIndex, int childIndex) {
		if (userDatas != null && userDatas.get(groupIndex).size() > childIndex) {

		}
	}

	@Override
	public int getGroupCount() {
		return (grouDatas != null) ? grouDatas.size() : 0;
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		if (userDatas != null) {
			if (userDatas.size() > groupPosition
					&& userDatas.get(groupPosition) != null) {
				userDatas.get(groupPosition).size();
			} else {
				return 0;
			}
		} else {
			return 0;
		}
		return (userDatas != null) ? userDatas.get(groupPosition).size() : 0;
	}

	@Override
	public Object getGroup(int groupPosition) {
		return (grouDatas != null) ? grouDatas.get(groupPosition) : null;
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getGroupId(int groupPosition) {
		// TODO Auto-generated method stub
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded,
			View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if (convertView != null) {
			tempView = convertView;
		} else {
			tempView = mInflater.inflate(R.layout.ex_list_group_item, parent,
					false);
		}
		groupHolder = (GroupViewHolder) tempView.getTag();
		if (groupHolder == null) {
			groupHolder = new GroupViewHolder();
			groupHolder.nameTextView = (TextView) tempView
					.findViewById(R.id.group_name);
			groupHolder.rightBtn = (Button) tempView
					.findViewById(R.id.group_rightbtn);
			groupHolder.rightBtn.setOnClickListener(onclickListener);
			groupHolder.btDelete = (Button) tempView
					.findViewById(R.id.group_delete_btn);
			groupHolder.btDelete.setOnClickListener(onclickListener);
			groupHolder.btUpdateName = (Button) tempView
					.findViewById(R.id.group_update_name_btn);
			groupHolder.btUpdateName.setOnClickListener(onclickListener);
			tempView.setTag(groupHolder);
		}
		groupHolder.rightBtn.setTag(String.valueOf(groupPosition));
		groupHolder.btDelete.setTag(String.valueOf(groupPosition));
		groupHolder.btUpdateName.setTag(String.valueOf(groupPosition));
		gdata = grouDatas.get(groupPosition);
		groupHolder.nameTextView.setText(gdata.getName());
		return tempView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
			boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView != null) {
			tempView = convertView;
		} else {
			tempView = mInflater.inflate(R.layout.ex_list_child_item, parent,
					false);
		}
		childHolder = (ChildViewHolder) tempView.getTag();
		if (childHolder == null) {
			childHolder = new ChildViewHolder();
			childHolder.nameTextView = (TextView) tempView
					.findViewById(R.id.child_name);
			childHolder.idTextView = (TextView) tempView
					.findViewById(R.id.child_id);
			childHolder.delBtn = (Button) tempView
					.findViewById(R.id.child_rightbtn);
			childHolder.delBtn.setOnClickListener(onclickListener);
			childHolder.btUpdatePWD = (Button) tempView
					.findViewById(R.id.child_update_pwd_btn);
			childHolder.btUpdatePWD.setOnClickListener(onclickListener);
			tempView.setTag(childHolder);
		}
		cdata = userDatas.get(groupPosition).get(childPosition);
		childHolder.delBtn.setTag(groupPosition + "," + childPosition);
		childHolder.btUpdatePWD.setTag(groupPosition + "," + childPosition);
		childHolder.nameTextView.setVisibility(View.VISIBLE);
		childHolder.nameTextView.setText(cdata.getUserName());
		childHolder.idTextView.setVisibility(View.VISIBLE);
		childHolder.idTextView.setText(cdata.getId());

		return tempView;
	}

	public class GroupViewHolder {
		private TextView nameTextView;
		private Button rightBtn;
		private Button btDelete;
		private Button btUpdateName;
	}

	public class ChildViewHolder {
		private TextView nameTextView;
		private TextView idTextView;
		private Button btUpdatePWD;
		private Button delBtn;
	}
}
